package com.btl.service.ai.support.input.common.parse.parsers;

import dev.langchain4j.data.document.BlankDocumentException;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentParser;
import dev.langchain4j.data.document.Metadata;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * Parses HTML file into a {@link Document} using Jsoup library
 */
public class HtmlDocumentParser implements DocumentParser {

    private final boolean includeMetadata;

    public HtmlDocumentParser() {
        this(false);
    }

    public HtmlDocumentParser(boolean includeMetadata) {
        this.includeMetadata = includeMetadata;
    }

    @Override
    public Document parse(InputStream inputStream) {
        try {
            org.jsoup.nodes.Document jsoupDoc = Jsoup.parse(inputStream, StandardCharsets.UTF_8.name(), "");
            String text = jsoupDoc.text();
            if (text.trim()
                    .isEmpty()) {
                throw new BlankDocumentException();
            }

            if (includeMetadata) {
                return Document.from(text, extractMetadata(jsoupDoc));
            } else {
                return Document.from(text);
            }

        } catch (IOException e) {
            throw new RuntimeException("Failed to parse HTML document", e);
        }
    }

    private Metadata extractMetadata(org.jsoup.nodes.Document jsoupDoc) {
        Metadata metadata = new Metadata();
        // 添加 <title> 标签
        String title = jsoupDoc.title();
        if (!title.isEmpty()) {
            metadata.put("title", title);
        }
        for (Element meta : jsoupDoc.select("meta[name]")) {
            String name = meta.attr("name");
            String content = meta.attr("content");
            if (!name.isEmpty() && !content.isEmpty()) {
                metadata.put(name, content);
            }
        }

        return metadata;
    }
}
